package speed2010.matcher.gui.controls;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import speed2010.matcher.gui.FOntologyViewer;
import speed2010.matcher.gui.util.OwlFilter;
import speed2010.matcher.util.FormatHelper;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class UBrowse extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JLabel lblHeaderText;
	private JButton btnBrowse;
	private JTextField txtFilePath;
	
	private int fileOrDirectory;
	
	//private static String lastAccessedDirectory = "C:\\Z\\CIn\\workspace\\eclipse34\\OntologyMatcher\\owls\\06022009_Damires";
	private static String lastAccessedDirectory = "C:\\ThiagoPacheco\\eclipse34\\workspace\\OntologyMatcher\\owls\\06022009_Damires";
	private JButton btnView = null;
	
	public UBrowse()
	{
	
	}
	
	public UBrowse(int fileOrDirectory)
	{
		super();
		this.getBtnView().setVisible(false);
		this.fileOrDirectory = fileOrDirectory;
		this.initialize();
	}
	
	public UBrowse(int fileOrDirectory, boolean view)
	{
		this(fileOrDirectory);
		this.getBtnView().setVisible(view);
	}
	
	private void initialize() {
			
		this.setPreferredSize(new java.awt.Dimension(328, 59));
		this.setLayout(null);
		this.setMinimumSize(new java.awt.Dimension(328, 59));
		this.setSize(new Dimension(361, 59));
		this.add(getTxtFilePath());
		this.add(getBtnBrowse());
		this.add(getLblHeaderText());

		this.add(getBtnView(), null);
	}
			
	private JButton getBtnBrowse()
	{
		if(btnBrowse == null)
		{
			btnBrowse = new JButton();
			btnBrowse.setText("Browse...");
			btnBrowse.setBounds(166, 25, 90, 22);
			btnBrowse.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
										
					JFileChooser fileChooser = new JFileChooser();	
					
					fileChooser.setCurrentDirectory(new File(lastAccessedDirectory));
					fileChooser.setFileSelectionMode(fileOrDirectory);
					fileChooser.setFileFilter(new OwlFilter());
					int returnValue = fileChooser.showOpenDialog(null);
										
					if(returnValue == JFileChooser.APPROVE_OPTION)
					{
						setEnabled(false);
						
						String path = fileChooser.getSelectedFile().getAbsolutePath();
						
						if(JFileChooser.FILES_ONLY == fileOrDirectory && !FormatHelper.isValidOntologyFile(path))
						{
							JOptionPane
							.showMessageDialog(
									null,
									fileChooser.getSelectedFile().getName() + " is not a valid owl file.",
									"ERROR",
									JOptionPane.ERROR_MESSAGE);
						}
						else
						{
							lastAccessedDirectory = fileChooser.getSelectedFile().getParent();
							txtFilePath.setText(path);
							getBtnView().setEnabled(true);
						}
						
						setEnabled(true);
					}
				}
			});
		}
		return btnBrowse;
	}
			
	private JTextField getTxtFilePath()
	{
		if(txtFilePath == null)
		{
			txtFilePath = new JTextField();
			txtFilePath.setBounds(12, 25, 147, 22);
			txtFilePath.setEditable(false);
		}
		return txtFilePath;
	}
	
	public void setFilePath(String text)
	{
		txtFilePath.setText(text);
	}
			
	private JLabel getLblHeaderText() {
		if(lblHeaderText == null) {
			lblHeaderText = new JLabel();
			lblHeaderText.setText("");
			lblHeaderText.setBounds(12, 3, 233, 16);
		}
		return lblHeaderText;
	}

	public String getFilePath()
	{
		return txtFilePath.getText();
	}

	public boolean isFilePathFilled()
	{
		boolean isFilePathFilled = false;
		String text = txtFilePath.getText();
		if(text != null && text.length() > 0)
			isFilePathFilled = true;
		
		return isFilePathFilled;
	}
	
	public void setHeaderText(String headerText)
	{
		lblHeaderText.setText(headerText);
	}
	
	@Override
	public void setEnabled(boolean enabled) {
		super.setEnabled(enabled);		
		btnBrowse.setEnabled(enabled);
	}

	/**
	 * This method initializes btnView	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnView() {
		if (btnView == null) {
			btnView = new JButton();
			btnView.setBounds(new Rectangle(262, 25, 65, 22));
			btnView.setText("View");
			btnView.setEnabled(false);
			btnView.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					FOntologyViewer fViewer = new FOntologyViewer(getFilePath());
					fViewer.setVisible(true);
				}				
			});
		}
		return btnView;
	}
	
}  //  @jve:decl-index=0:visual-constraint="10,10"
